package com.mxpio.erp.equipment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mxpio.erp.common.equipment.entity.EquipmentCommParametersDetail;
import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepDetail;
import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepDetailParam;
import com.mxpio.erp.common.equipment.service.EquipmentCommParametersDetailService;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepDetailParamService;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepDetailService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 保养类别Service业务层处理
 *
 * @author Cheng
 * @date 2023-07-18
 */
@Service
public class EquipmentUpkeepDetailServiceImpl extends BaseServiceImpl<EquipmentUpkeepDetail> implements EquipmentUpkeepDetailService {

    @Resource
    private EquipmentCommParametersDetailService equipmentCommParametersDetailService;

    @Resource
    private EquipmentUpkeepDetailParamService equipmentUpkeepDetailParamService;

    @Override
    @Transactional(readOnly = true)
    public List<EquipmentUpkeepDetail> selectByMainId(String mainId) {
        return JpaUtil.linq(EquipmentUpkeepDetail.class).equal("categoryId", mainId).list();
    }

    @Override
    @Transactional
    public void saveFromParams(EquipmentUpkeepDetail equipmentUpkeepDetail) {
        this.save(equipmentUpkeepDetail);
        if (equipmentUpkeepDetail.getChooseEnable() == 1) {
            List<EquipmentCommParametersDetail> commParametersDetails = equipmentCommParametersDetailService.selectByMainId(equipmentUpkeepDetail.getParamId());
            if (!CollectionUtils.isEmpty(commParametersDetails)) {
                commParametersDetails.forEach(params -> {
                    EquipmentUpkeepDetailParam upkeepDetailParam = new EquipmentUpkeepDetailParam();
                    BeanUtil.copyProperties(params, upkeepDetailParam, "id", "createBy", "createTime", "updateBy", "updateTime", "createDept");
                    upkeepDetailParam.setCommParamId(params.getId());
                    upkeepDetailParam.setDetailId(equipmentUpkeepDetail.getId());
                    JpaUtil.save(upkeepDetailParam);
                });
            }
        }

    }

    @Override
    @Transactional
    public void editFromParams(EquipmentUpkeepDetail equipmentUpkeepDetail) {
        // 更新明细主内容
        this.update(equipmentUpkeepDetail);

        // 删除明细选项内容
        equipmentUpkeepDetailParamService.deleteByMainId(equipmentUpkeepDetail.getId());

        if (equipmentUpkeepDetail.getChooseEnable() == 1) {
            List<EquipmentCommParametersDetail> commParametersDetails = equipmentCommParametersDetailService.selectByMainId(equipmentUpkeepDetail.getParamId());
            if (!CollectionUtils.isEmpty(commParametersDetails)) {
                commParametersDetails.forEach(params -> {
                    EquipmentUpkeepDetailParam upkeepDetailParam = new EquipmentUpkeepDetailParam();
                    BeanUtil.copyProperties(params, upkeepDetailParam, "id", "createBy", "createTime", "updateBy", "updateTime", "createDept");
                    upkeepDetailParam.setCommParamId(params.getId());
                    upkeepDetailParam.setDetailId(equipmentUpkeepDetail.getId());
                    JpaUtil.save(upkeepDetailParam);
                });
            }
        }
    }
}
